package com.example.kaogong.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.kaogong.common.utils.RespBean;
import com.example.kaogong.common.utils.SensitiveWordFilter;
import com.example.kaogong.sys.mapper.UserMapper;
import com.example.kaogong.sys.pojo.CourseComment;
import com.example.kaogong.sys.mapper.CourseCommentMapper;
import com.example.kaogong.sys.pojo.User;
import com.example.kaogong.sys.pojo.UserLike;
import com.example.kaogong.sys.pojo.dto.CourseCommentDto;
import com.example.kaogong.sys.pojo.vo.CourseCommentVo;
import com.example.kaogong.sys.service.ICourseCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.time.LocalDateTime;

/**
 * <p>
 * 课程讨论 服务实现类
 * </p>
 *
 * @author    
 * @since 2022-03-31
 */
@Service
public class CourseCommentServiceImpl extends ServiceImpl<CourseCommentMapper, CourseComment> implements ICourseCommentService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CourseCommentMapper courseCommentMapper;

    @Autowired
    private SensitiveWordFilter sensitiveWordFilter;

    /**
     * 当前登录用户对某一门课程进行评论
     * @param courseCommentDto
     * @param name
     * @return
     */
    @Override
    public RespBean addCourseComment(CourseCommentDto courseCommentDto, String name) {
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("username", name));
        if (null == user)
            return RespBean.error();

        CourseComment courseComment = new CourseComment();
        courseComment.setCourseId(courseCommentDto.getCourseId());
        // 敏感词过滤
        courseComment.setContent(sensitiveWordFilter.filter(courseCommentDto.getContent()));
        courseComment.setPostTime(LocalDateTime.now());
        courseComment.setUid(user.getUid());
        courseComment.setReplyId(courseCommentDto.getReplyId());
        courseComment.setTargetId(courseCommentDto.getTargetId());
        int insert = courseCommentMapper.insert(courseComment);
        if (insert < 1)
            return RespBean.error();
        return RespBean.success();
    }

    /**
     * 分页获取课程评论
     * @param current
     * @param size
     * @param courseId
     * @param commentId
     * @return
     */
    @Override
    public RespBean<Page<CourseCommentVo>> getCourseComment(Long current, Long size, Integer courseId, Integer commentId) {
        Page<CourseCommentVo> courseCommentVoPage = courseCommentMapper.getCourseComment(new Page<CourseCommentVo>(current, size), courseId, commentId);
        courseCommentVoPage.getRecords().forEach(courseCommentVo -> {
            //回复数
            Integer count = courseCommentMapper.selectCount(new QueryWrapper<CourseComment>().eq("reply_id", courseCommentVo.getCommentId()));
            courseCommentVo.setCount(count);
        });
        return RespBean.success(courseCommentVoPage);
    }

    /**
     * 管理员屏蔽某条评论
     * @param commentId
     * @return
     */
    @Override
    public RespBean shieldComment(Integer commentId) {
        int row = courseCommentMapper.shieldComment(commentId);
        if (row < 1)
            return RespBean.error();
        return RespBean.success();
    }

    /**
     * 当前登录用户删除自己的一条评论
     * @param commentId
     * @param name
     * @return
     */
    @Override
    public RespBean deleteComment(Integer commentId, String name) {
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("username", name));
        if (null == user)
            return RespBean.error();
        int delete = courseCommentMapper.delete(new QueryWrapper<CourseComment>().eq("comment_id", commentId).eq("uid", user.getUid()));
        if (delete < 1)
            return RespBean.error();
        return RespBean.success();
    }

    /**
     * 当前登录用户点赞一条评论
     * @param commentId
     * @param name
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RespBean likeComment(Integer commentId, String name) {
        try {
            User user = userMapper.selectOne(new QueryWrapper<User>().eq("username", name));
            if (null == user)
                return RespBean.error();

            UserLike userLike = new UserLike(user.getUid(), commentId);
            int row = courseCommentMapper.insertUserCourseCommentLike(userLike);
            if (row < 1)
                return RespBean.error();
            int addLike = courseCommentMapper.likeComment(commentId);
            if (addLike < 1)
                return RespBean.error();

        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return RespBean.error();
        }
        return RespBean.success();
    }
}
